/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cj.mobile.iabp.mvp.ui.adapter;

import android.view.View;

import androidx.annotation.NonNull;

import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.utils.CommonUtils;
import com.cj.mobile.iabp.mvp.ui.holder.CameraContentItemHolder;
import com.cj.mobile.iabp.mvp.ui.holder.CameraItemHolder;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * ================================================
 * 上传凭证适配器
 * ================================================
 */
public class UploadCertificateAdapter extends DefaultAdapter<String> {
    // 图片内容
    private final int VIEW_CAMERA_CONTENT_TYPE = 0;
    // 上传图片
    private final int VIEW_CAMERA_TYPE = 1;

    // 图片类型
    private int type = 0;
    // 是否使用默认背景
    private boolean isDefaultBg = true;

    public UploadCertificateAdapter(List<String> infos) {
        super(infos);
    }

    public UploadCertificateAdapter(List<String> infos, int type) {
        super(infos);
        this.type = type;
    }

    public void setDefaultBg(boolean defaultBg) {
        isDefaultBg = defaultBg;
    }

    /**
     * 清理缓存
     */
    public void onDestroy() {
        if (mInfos != null) {
            mInfos.clear();
            mInfos = null;
        }
    }

    private boolean isCamera() {
        int size = this.getInfos().size();

        if (size < 3) {
            // 显示拍照
            return true;
        } else {
            // 不显示拍照
            return false;
        }
    }

    @Override
    public int getItemCount() {
        int size = this.getInfos().size();
        int count = 0;

        // 增加图片位置
        if (isCamera()) {
            count = 1 + size;
        } else {
            count = size;
        }

        return count;
    }

    @NonNull
    @Override
    public BaseHolder<String> getHolder(@NonNull View v, int viewType) {
        if (viewType == VIEW_CAMERA_TYPE) {
            return new CameraItemHolder(v);
        } else {
            return new CameraContentItemHolder(v);
        }
    }

    @Override
    public void onBindViewHolder(BaseHolder<String> holder, int position) {
        if (holder instanceof CameraContentItemHolder) {
            ((CameraContentItemHolder) holder).initData(type, isDefaultBg);
            ((CameraContentItemHolder) holder).setData(getInfos().get(position), position);
        } else if (holder instanceof CameraItemHolder) {
            ((CameraItemHolder) holder).initData(type, isDefaultBg);
            ((CameraItemHolder) holder).setData("", position);
        }
    }

    @Override
    public int getLayoutId(int viewType) {
        if (viewType == VIEW_CAMERA_TYPE) {
            return R.layout.item_camera_layout;
        } else {
            return R.layout.item_camera_a_layout;
        }
    }

    @Override
    public int getItemViewType(int position) {
        int size = getInfos().size();

        // 计算图片内容
        if (size > 0) {
            if (isCamera()) {
                // 未满9张，显示“拍照按钮”

                // 计算图片图片位置
                if (position < size) {
                    return VIEW_CAMERA_CONTENT_TYPE;
                }

                // 计算拍照位置
                if (position == size) {
                    return VIEW_CAMERA_TYPE;
                }

            } else {
                // 已满9张，不显示“拍照按钮”
                return VIEW_CAMERA_CONTENT_TYPE;
            }
        } else {
            return VIEW_CAMERA_TYPE;
        }

        return -1;
    }

    /**
     * 多张
     */
    public void setData(@NonNull ArrayList<String> infos) {
        if (CommonUtils.isNotEmpty(infos))
            this.mInfos.addAll(infos);
    }

    public void setData(@NonNull String[] path) {
        if (path.length > 0) {
            this.mInfos = new ArrayList<>(Arrays.asList(path));
        } else {
            this.mInfos = new ArrayList<>();
        }

    }

    /**
     * 单张
     */
    public void setDataLeaflet(String val) {
        if (mInfos != null) {
            this.mInfos.add(val);
        }

    }

    public ArrayList<String> getInfos() {
        return (ArrayList<String>) mInfos;
    }
}
